﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FourWayClimbingDemo.Chess.Advice
{
    public sealed class CurrentAdvice : BaseAdvice
    {
        private ChessBoard _chessBoard;
        private Player _player;
        private ChessPiece _chessPiece;

        public CurrentAdvice(ChessBoard chessBoard, Player player, ChessPiece chessPiece) {
            this._chessBoard = chessBoard;
            this._player = player;
            this._chessPiece = chessPiece;
        }


        public override int Execute()
        {
            AdviceStatusEnum status = AdviceStatusEnum.None;

            var posLeft = _chessBoard.GetChessPieceAround(_chessPiece,Dir.LEFT);
            var posRight = _chessBoard.GetChessPieceAround(_chessPiece, Dir.RIGHT);

            var posTop = _chessBoard.GetChessPieceAround(_chessPiece, Dir.TOP);
            var posBottom = _chessBoard.GetChessPieceAround(_chessPiece, Dir.BOTTOM);

            if (posLeft != null && posRight != null) {
                if (_chessBoard.IsEmpty(posLeft.Value) && !_chessBoard.IsEmpty(posRight.Value) && !_player.IsMyChessPiece(posRight.Value))
                {
                    status = CompareAdviceStatus(status, AdviceStatusEnum.当前_被吃);
                }
                else if (_chessBoard.IsEmpty(posRight.Value) && !_chessBoard.IsEmpty(posLeft.Value) && !_player.IsMyChessPiece(posLeft.Value))
                {
                    status = CompareAdviceStatus(status, AdviceStatusEnum.当前_被吃);
                }
                else {
                    status = CompareAdviceStatus(status, AdviceStatusEnum.当前_安全);
                }
            }

            if (posTop != null && posBottom != null)
            {
                if (_chessBoard.IsEmpty(posTop.Value) && !_chessBoard.IsEmpty(posBottom.Value) && !_player.IsMyChessPiece(posBottom.Value))
                {
                    status = CompareAdviceStatus(status, AdviceStatusEnum.当前_被吃);
                }
                else if (_chessBoard.IsEmpty(posBottom.Value) && !_chessBoard.IsEmpty(posTop.Value) && !_player.IsMyChessPiece(posTop.Value))
                {
                    status = CompareAdviceStatus(status, AdviceStatusEnum.当前_被吃);
                }
                else
                {
                    status = CompareAdviceStatus(status, AdviceStatusEnum.当前_安全);
                }
            }

            _score += GetScore(status);

            return base.Execute();
        }
    }
}
